﻿using System;
using System.IO;
using YArchitech.LIB;

namespace RegionSplittor.Utils
{
	public class SmokeFormConfigManager
	{
		public static double GetWellWordCellOffsetDistance()
		{
			return SmokeFormConfigManager.GetDistanceByType(ConfigDistanceType.CDT_WellWordCell);
		}

		public static double GetOneWordCellOffsetDistance()
		{
			return SmokeFormConfigManager.GetDistanceByType(ConfigDistanceType.CDT_OneWordCell);
		}

		public static OneWordLayoutType GetOneWordCellLayoutType()
		{
			OneWordLayoutType result = OneWordLayoutType.OWLT_NotValid;
			XmlHelperForElectric xmlParamNode = SmokeFormConfigManager.GetXmlParamNode();
			if (xmlParamNode == null)
			{
				return result;
			}
			string attribute = xmlParamNode.GetAttribute(SmokeFormConfigManager.strYzCbText);
			if (attribute == null)
			{
				return result;
			}
			if (attribute.Equals(SmokeFormConfigManager.strTopLft))
			{
				result = OneWordLayoutType.OWLT_TopLft;
			}
			else if (attribute.Equals(SmokeFormConfigManager.strBtmRht))
			{
				result = OneWordLayoutType.OWLT_BtmRht;
			}
			else
			{
				result = OneWordLayoutType.OWLT_NotValid;
			}
			return result;
		}

		public static double GetOddNumberColumnOffsetDistance()
		{
			return SmokeFormConfigManager.GetDistanceByType(ConfigDistanceType.CDT_OddNumberColumn);
		}

		public static double GetEvenNumberColumnOffsetDistance()
		{
			return SmokeFormConfigManager.GetDistanceByType(ConfigDistanceType.CDT_EvenNumberColumn);
		}

		public static bool IsConfigDiagonalLayout()
		{
			bool result = false;
			XmlHelperForElectric xmlParamNode = SmokeFormConfigManager.GetXmlParamNode();
			if (xmlParamNode == null)
			{
				return result;
			}
			return SmokeFormConfigManager.StrToBoolean(xmlParamNode.GetAttribute(SmokeFormConfigManager.strRd));
		}

		public static bool IsConfigProductLayout()
		{
			return !SmokeFormConfigManager.IsConfigDiagonalLayout();
		}

		private static XmlHelperForElectric GetXmlParamNode()
		{
			if (!File.Exists(SmokeFormConfigManager.m_strFullPath))
			{
				return null;
			}
			XmlHelperForElectric result;
			try
			{
				XmlHelperForElectric xmlHelperForElectric = new XmlHelperForElectric();
				xmlHelperForElectric.LoadXml(SmokeFormConfigManager.m_strFullPath, SmokeFormConfigManager.Root);
				if (!xmlHelperForElectric.SelectNode(Path.Combine(SmokeFormConfigManager.Root, SmokeFormConfigManager.Node, SmokeFormConfigManager.ChildNode)))
				{
					result = null;
				}
				else
				{
					result = xmlHelperForElectric;
				}
			}
			catch
			{
				result = null;
			}
			return result;
		}

		private static double GetDistanceByType(ConfigDistanceType type)
		{
			double result = 0.0;
			XmlHelperForElectric xmlParamNode = SmokeFormConfigManager.GetXmlParamNode();
			if (xmlParamNode == null)
			{
				return result;
			}
			switch (type)
			{
			case ConfigDistanceType.CDT_WellWordCell:
				result = SmokeFormConfigManager.StrToDouble(xmlParamNode.GetAttribute(SmokeFormConfigManager.strJzText));
				break;
			case ConfigDistanceType.CDT_OneWordCell:
				result = SmokeFormConfigManager.StrToDouble(xmlParamNode.GetAttribute(SmokeFormConfigManager.strYzText));
				break;
			case ConfigDistanceType.CDT_OddNumberColumn:
				result = SmokeFormConfigManager.StrToDouble(xmlParamNode.GetAttribute(SmokeFormConfigManager.strSzJsText));
				break;
			case ConfigDistanceType.CDT_EvenNumberColumn:
				result = SmokeFormConfigManager.StrToDouble(xmlParamNode.GetAttribute(SmokeFormConfigManager.strSzOsText));
				break;
			}
			return result;
		}

		private static double StrToDouble(string str)
		{
			double result = 0.0;
			double num;
			if (double.TryParse(str, out num))
			{
				result = num;
			}
			return result;
		}

		private static bool StrToBoolean(string str)
		{
			bool result = false;
			bool flag;
			if (bool.TryParse(str, out flag))
			{
				result = flag;
			}
			return result;
		}

		public static string m_strFullPath = Path.Combine(Product.WorkingLocation, "FormSmokeDetectorConfig.xml");

		public static string Root = "Root";

		public static string Node = "FormSmokeDetectorConfig";

		public static string ChildNode = "Params";

		public static string LocationX = "X";

		public static string LocationY = "Y";

		public static string strJzText = "strJzText";

		public static string strYzCbText = "strYzCbText";

		public static string strYzText = "strYzTbText";

		public static string strSzJsText = "strSzJsText";

		public static string strSzOsText = "strSzOsText";

		public static string strRd = "strRd";

		public static string strTopLft = "上/左";

		public static string strBtmRht = "下/右";
	}
}
